package com.sunkai.math;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

public class GetObjectImpl implements GetObject {

	/**
	 * 
	 * @date 2018年2月6日 下午7:30:28
	 * @param str  
	 obj.age=10
	 obj.name=nihao
	 obj.box.title=zhidaole
	 obj.box.body=hhhhhh
	 * 
	 * @param clazz xobj.class
	 
	 class xobj{
	 	private String name;
	 	private int age;
	 	private Box box;
	 }
	 class Box{
	 	private String title;
	 	private String body;
	 }
	 
	 * @return
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	@Override
	public <T> T getObject(String str, Class<T> clazz) throws Exception {
		if (str == null || str.trim().length() == 0) {
			throw new RuntimeException(" string is null ");
		}
		String osName = System.getProperty("os.name").toLowerCase();
		String[] array = null;
		if (osName.contains("win")) {
			array = str.split("\r\n");
		} else if (osName.contains("linux")) {
			array = str.split("\n");
		}
		System.out.println(Arrays.toString(array));
		//封装成map
		Map<String, String> map = new LinkedHashMap<String, String>(array.length); 
		for (int i=0;i<array.length;i++) {
			String kv = array[i];
			int index = kv.indexOf("=");
			String key = kv.substring(0, index);
			String value = kv.substring(index+1);
			map.put(key, value);
		}
		System.out.println(map);
		
		//obj.age=10
		//obj.name=nihao
		//obj.box.title=zhidaole
		//obj.box.body=hhhhhh
		Set<Entry<String, String>> entrySet = map.entrySet();
/*		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			
			String [] fieldArr = key.split(".");
			int count = fieldArr.length - 1;
			for (int i=1; i <= count; i++) {
				String fieldName = fieldArr[i];
			}
			String fieldName = key.substring(key.indexOf(".")+1);
			System.out.println(fieldName);
			
		}*/
		Object obj = clazz.newInstance();
		Map<String, Object> objMap = new LinkedHashMap<String, Object>();
		for (Entry<String, String> entry : entrySet) {
			String key = entry.getKey();
			String value = entry.getValue();
			String [] fieldArr = key.split("\\.");
			int count = fieldArr.length - 1;
			if (count <= 1) {
				String fieldName = fieldArr[count];
				Field field = clazz.getDeclaredField(fieldName);
				System.out.println(fieldName);
				field.setAccessible(true);
				Class type = field.getType();
				//根数属性类型设置Field属性值
				setFiledByType(obj, field, type, value);
			} else {
				for (int i=1;i<count;i++) {
					String fieldName = fieldArr[i];
					Field field = clazz.getDeclaredField(fieldName);
					field.setAccessible(true);
					Class inClass = field.getType();
					Object inObj = null;
					if (objMap.get(fieldName) != null) {
						inObj = objMap.get(fieldName);
					} else {
						inObj = inClass.newInstance();
						objMap.put(fieldName, inObj);
						field.set(obj, inObj);
					}
					Field inField = inClass.getDeclaredField(fieldArr[i+1]);//ccc
					inField.setAccessible(true);
					setFiledByType(inObj, inField, inField.getType(), value);
					
					
				}
			}
			
			
		}
		return (T)obj;
	}
	
	private void setFiledByType(Object obj, Field field, Class type, String value) throws NumberFormatException, IllegalArgumentException, IllegalAccessException {
		if (type == int.class || type == Integer.class) {
			field.setInt(obj, Integer.parseInt(value));
		} else if (type == long.class || type == Long.class) {
			field.setLong(obj, Long.parseLong(value));
		} else if (type == short.class || type == Short.class) {
			field.setShort(obj, Short.parseShort(value));
		} else if (type == byte.class || type == Byte.class) {
			field.setByte(obj, Byte.parseByte(value));
		} else if (type == float.class || type == Float.class) {
			field.setFloat(obj, Float.parseFloat(value));
		} else if (type == double.class || type == Double.class) {
			field.setDouble(obj, Double.parseDouble(value));
		} else if (type == boolean.class || type == Boolean.class) {
			field.setBoolean(obj, Boolean.parseBoolean(value));
		} else {
			field.set(obj, value);
		}
	}

	public static void main(String[] args) throws Exception {
		String src = "obj.age=10\r\nobj.name=nihao\r\nobj.box.title=zhidaole\r\nobj.box.body=hhhhhh\r\nobj.box.ccc.sex=999";
		GetObject g = new GetObjectImpl();
		Obj obj = g.getObject(src, Obj.class);
		System.out.println(obj);
	}
}
